/**
 * 详情对话框
 */
var BtyZxBaoxiaodanAuditInfoDlg = {
    data: {
        id: "",
        shopName: "",
        userName: "",
        userId: "",
        productRemark: "",
        applyDate: "",
        auditTime: "",
        reason: "",
        status: "",
        fileUrl: "",
        bxdaId: "",
        createTime: "",
        createUser: "",
        updateTime: "",
        updateUser: "",
        reimbursementAmount: "",
        remitAmount: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/btyZxBaoxiaodanAudit/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('btyZxBaoxiaodanAuditForm', result.data);

    $('#reason').html(result.data.reason);
    $('#shopName').html(result.data.shopName);
    $('#userName').html(result.data.userName);
    $('#productRemark').html(result.data.productRemark);
    $('#reimbursementAmount').html('¥'+result.data.reimbursementAmount);
    // 状态!=2 隐藏同意驳回按钮 打款金额变为只读
    if (result.data.status != 2) {
        $('#btnReject').hide();
        $('#btnAgree').hide();
        $('#remitAmountDiv').html(`<p id="remitAmount" style="padding-top: 6px;color: #F59A23;">¥${result.data.remitAmount}</p>`);
    }
    // 状态为工程待审核 input = 0
    if (result.data.status == 2) {
        $('#remitAmount').val("");
    }

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/btyZxBaoxiaodanAudit/editItem", function (data) {
            Feng.success("更新成功！");
            window.location.href = Feng.ctxPath + '/btyZxBaoxiaodanAudit'
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });
    // 状态 1.待提交，2.工程待审核，3.工程驳回，4.财务待审核，5.财务驳回，6.待打款，7.已打款；
    // 通过
    $('#btnAgree').click(function() {
        if ($('#remitAmount').val() == '') {
             Feng.error("请输入打款金额");
             return;
        }
        layer.confirm('是否确认通过此单据？', {icon: 3, title:'提示'}, function(index){
            $('.layui-layer-btn0').css('pointer-events','none');
            layer.close(index);
            var ajax = new $ax(Feng.ctxPath + "/btyZxBaoxiaodanAudit/editItem", function (data) {
                flag = false;
                Feng.success("通过成功！");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                //关掉对话框
                admin.closeThisDialog();
            }, function (data) {
                Feng.error("通过失败！" + data.responseJSON.message)
            });
            ajax.set("id", Feng.getUrlParam("id"));
            ajax.set("remitAmount", $('#remitAmount').val());
            ajax.set("status", 4);
            ajax.start();
        });
//        var operation = function () {
//            var DISABLED = 'layui-btn-disabled';
//            $(':button').addClass(DISABLED); // 添加样式
//            $(':button').attr('disabled', 'disabled');  // 添加属性
//            setTimeout(function() {
//            }, 1000000)
//            var ajax = new $ax(Feng.ctxPath + "/btyZxBaoxiaodanAudit/editItem", function (data) {
//                flag = false;
//                Feng.success("通过成功！");
//                //传给上个页面，刷新table用
//                admin.putTempData('formOk', true);
//                //关掉对话框
//                admin.closeThisDialog();
//            }, function (data) {
//                Feng.error("通过失败！" + data.responseJSON.message)
//            });
//            ajax.set("id", Feng.getUrlParam("id"));
//            ajax.set("remitAmount", $('#remitAmount').val());
//            ajax.set("status", 4);
//            ajax.start();
//        };
//        Feng.confirm("是否确认通过此单据？", operation);
    })
    // 拒绝
    $('#btnReject').click(function() {
        layer.prompt({
          formType: 2,
          value: '',
          title: '请输入驳回原因',
        }, function(value, index, elem){
          var ajax = new $ax(Feng.ctxPath + "/btyZxBaoxiaodanAudit/editItem", function (data) {
              Feng.success("驳回成功！");
              //传给上个页面，刷新table用
              admin.putTempData('formOk', true);
              //关掉对话框
              admin.closeThisDialog();
          }, function (data) {
              Feng.error("驳回失败！" + data.responseJSON.message)
          });
          ajax.set("id", Feng.getUrlParam("id"));
          ajax.set("reason", value);
          ajax.set("status", 3);
          ajax.start();
          layer.close(index);
        });
    })

    $('#download').click(function(){
        downloadByUrl({
          url: Feng.ctxPath + result.data.fileUrl,
          target: '_self',
          fileName: "货品单",
        });
    });

    function openWindow(url, opt) {
        const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
        const feature = [];
        noopener && feature.push('noopener=yes');
        noreferrer && feature.push('noreferrer=yes');
        window.open(url, target, feature.join(','));
    }
    function downloadByUrl({
      url,
      target = '_blank',
      fileName,
    }) {
      const isChrome = window.navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
      const isSafari = window.navigator.userAgent.toLowerCase().indexOf('safari') > -1;

      if (/(iP)/g.test(window.navigator.userAgent)) {
        console.error('Your browser does not support download!');
        return false;
      }
      if (isChrome || isSafari) {
        const link = document.createElement('a');
        link.href = url;
        link.target = target;

        if (link.download !== undefined) {
          link.download = fileName || url.substring(url.lastIndexOf('/') + 1, url.length);
        }

        if (document.createEvent) {
          const e = document.createEvent('MouseEvents');
          e.initEvent('click', true, true);
          link.dispatchEvent(e);
          return true;
        }
      }
      if (url.indexOf('?') === -1) {
        url += '?download';
      }

      openWindow(url, { target });
      return true;
    }
});